// 风水住宅功能
function initFengshuiModule() {
  const fengshuiForm = document.getElementById('fengshuiForm');
  if (!fengshuiForm) return;
  
  fengshuiForm.addEventListener('submit', function(e) {
    e.preventDefault();
    
    // 检查登录状态
    if (checkNeedLogin()) return;
    
    const houseType = document.getElementById('houseType').value;
    const houseDirection = document.getElementById('houseDirection').value;
    const houseLayout = document.getElementById('houseLayout').value;
    const houseConcerns = document.getElementById('houseConcerns').value;
    
    // 提交风水分析请求
    fengshuiAPI.submitFengshuiAnalysis(userToken, {
      house_type: houseType,
      house_direction: houseDirection,
      house_layout: houseLayout,
      house_concerns: houseConcerns
    })
      .then(response => {
        if (response.code === 200) {
          // 显示基础分析结果
          showFengshuiResult(response.data);
          
          // 保存风水ID用于后续查询
          document.getElementById('fengshuiId').value = response.data.fengshuiId;
        } else {
          showToast(response.message || '提交风水分析失败');
        }
      })
      .catch(error => {
        showToast(error.message || '提交风水分析失败');
      });
  });
  
  // 查看详细分析按钮
  const viewDetailBtn = document.getElementById('viewFengshuiDetailBtn');
  if (viewDetailBtn) {
    viewDetailBtn.addEventListener('click', function() {
      const fengshuiId = document.getElementById('fengshuiId').value;
      if (!fengshuiId) {
        showToast('请先提交风水分析');
        return;
      }
      
      // 检查是否需要支付
      if (checkNeedPayment(false, 1.1)) {
        // 设置支付相关信息
        document.getElementById('paymentType').value = 'fengshui';
        document.getElementById('productId').value = fengshuiId;
        return;
      }
      
      // 获取详细分析
      fengshuiAPI.getFengshuiDetail(userToken, fengshuiId)
        .then(response => {
          if (response.code === 200) {
            showFengshuiDetailResult(response.data);
          } else {
            showToast(response.message || '获取详细分析失败');
          }
        })
        .catch(error => {
          showToast(error.message || '获取详细分析失败');
        });
    });
  }
}

// 显示风水基础分析结果
function showFengshuiResult(data) {
  const resultContainer = document.getElementById('fengshuiResultContainer');
  if (!resultContainer) return;
  
  resultContainer.style.display = 'block';
  
  const fengshuiInfo = document.getElementById('fengshuiInfo');
  const fengshuiAnalysis = document.getElementById('fengshuiAnalysis');
  
  if (fengshuiInfo) {
    fengshuiInfo.innerHTML = `
      <p class="text-lg font-medium">房屋类型：${data.houseType}</p>
      <p class="mt-2">朝向：${data.houseDirection}</p>
      <p>整体风水评分：${data.overallScore}/100</p>
    `;
  }
  
  if (fengshuiAnalysis) {
    fengshuiAnalysis.innerHTML = `
      <p class="mt-4">${data.basicAnalysis}</p>
      <p class="mt-2 text-gray-500">查看详细分析可获得更多风水改善建议</p>
    `;
  }
}

// 显示风水详细分析结果
function showFengshuiDetailResult(data) {
  const detailContainer = document.getElementById('fengshuiDetailContainer');
  if (!detailContainer) return;
  
  detailContainer.style.display = 'block';
  
  const detailContent = document.getElementById('fengshuiDetailContent');
  if (detailContent) {
    detailContent.innerHTML = `
      <h3 class="text-xl font-bold mb-4">详细风水分析</h3>
      <div class="space-y-4">
        <div>
          <h4 class="text-lg font-medium text-indigo-700">整体格局分析</h4>
          <p>${data.layoutAnalysis}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">朝向与采光</h4>
          <p>${data.directionAnalysis}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">客厅风水</h4>
          <p>${data.livingRoomAnalysis}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">卧室风水</h4>
          <p>${data.bedroomAnalysis}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">厨房风水</h4>
          <p>${data.kitchenAnalysis}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">卫生间风水</h4>
          <p>${data.bathroomAnalysis}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">财位分析</h4>
          <p>${data.wealthPositionAnalysis}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">改善建议</h4>
          <div class="bg-indigo-50 p-4 rounded-lg">
            ${data.improvementSuggestions.map(suggestion => `
              <div class="mb-2">
                <p class="font-medium">• ${suggestion.title}</p>
                <p class="text-gray-600 ml-4">${suggestion.description}</p>
              </div>
            `).join('')}
          </div>
        </div>
      </div>
    `;
  }
}

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
  initFengshuiModule();
});
